﻿using RimWorld;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;
using Verse.AI;
using Verse.Sound;

namespace Miao.Stand
{
    public class JobDriver_TheWorld : JobDriver
    {

       //这个效果的灵感来源于僵尸mod，我记得他是用协程来优化僵尸出生造成的产生。
        public IEnumerator TheWorldRun(StarPlatinumStand starPlatinumStand)
        {
            //yield return new WaitForSeconds(0.1f);
            // var timestop =  FleckMaker.GetDataStatic(pawn.PositionHeld.ToVector3(), pawn.Map, StandFleckDefOf.TimeStop);
            //pawn.Map.flecks.CreateFleck(timestop);
            FleckMaker.Static(pawn.PositionHeld, pawn.Map, StandFleckDefOf.TimeStop);
            StandSoundDefOf.TheWorldTimeStop.PlayOneShot(new TargetInfo(pawn.Position, pawn.Map));
            #region 尝试用巧方法继续时停渲染(失败)
            //string defName = starPlatinumStand.owner.def.defName;
            //ThingDef humanDef = DefDatabase<ThingDef>.GetNamed(defName+"Copy");
            //if (humanDef!=null)
            //{
            //    starPlatinumStand.owner.def = humanDef;
            //}
            #endregion
            while (starPlatinumStand.TheWorld)
            {
                //先修复时停朝向不变

                //stand.Map.MapUpdate();
                starPlatinumStand.Tick();
                starPlatinumStand.rotationTracker.UpdateRotation();
                starPlatinumStand.Drawer.ProcessPostTickVisuals(1);
                //starPlatinumStand.standAbilityTracker.Tick();
                //starPlatinumStand.DrawNowAt(starPlatinumStand.Position.ToVector3());//可以渲染，但原来的不会清除
                starPlatinumStand.owner.Tick();
                starPlatinumStand.owner.rotationTracker.UpdateRotation();
                starPlatinumStand.owner.Drawer.tweener.ResetTweenedPosToRoot();//卧槽，重设位置直接正常！
                //if (starPlatinumStand.owner.CurJobDef == StandJobDefOf.StandGhostFollow)
                //{
                //    starPlatinumStand.SetStandGhostPos();
                //}
                //starPlatinumStand.owner.DrawAt(dr.DrawPos);
                //stand.owner.DrawAt(stand.owner.Position.ToVector3());
                yield return null;
                if (starPlatinumStand.standPower.CurLevel < 0.1|| !starPlatinumStand.Spawned)//没能量取消theworld
                {
                    starPlatinumStand.TheWorld = false;
                    starPlatinumStand.standAbilityTracker.GetAbilitie<TheWorldAbility>().Continue = false;
                    Find.TickManager.CurTimeSpeed = TimeSpeed.Normal;
                    break;
                }
                else if (Find.TickManager.CurTimeSpeed != TimeSpeed.Paused)
                {
                    starPlatinumStand.standAbilityTracker.GetAbilitie<TheWorldAbility>().Continue = false;
                    starPlatinumStand.TheWorld = false;
                }
            }
            //yield return null;
            //starPlatinumStand.Drawer.tweener.ResetTweenedPosToRoot();
            starPlatinumStand.owner.Drawer.tweener.ResetTweenedPosToRoot();
            //var timestart = FleckMaker.GetDataStatic(pawn.Position.ToVector3(), pawn.Map, StandFleckDefOf.TimeStart);
            //pawn.Map.flecks.CreateFleck(timestart);
            FleckMaker.Static(pawn.PositionHeld, pawn.Map, StandFleckDefOf.TimeStart);
            StandSoundDefOf.StarPlatinumTheWorldTimeStart.PlayOneShot(new TargetInfo(starPlatinumStand.Position, starPlatinumStand.Map));
        }

        public override bool TryMakePreToilReservations(bool errorOnFailed)
        {
            return true;
        }
        protected override IEnumerable<Toil> MakeNewToils()
        {
            //ThingDef thingDef = DefDatabase<ThingDef>.GetNamed("Mote_PsychicEmanatorEffect");
            //Mote mote = MoteMaker.MakeStaticMote(this.pawn.Position.ToVector3(), this.Map, thingDef);
            //mote.Maintain();

            //FleckMaker.Static(pawn.Position, pawn.Map, FleckDefOf.PsycastAreaEffect, 2f);

            yield return Toils_General.Do(() =>
            {

                StandSoundDefOf.StarPlatinumTheWorld.PlayOneShot(new TargetInfo(pawn.Position, pawn.Map));
            });
            yield return Toils_General.WaitWith(TargetIndex.A, 120, true);
            //yield return Toils_General.WaitWith(TargetIndex.A, 10, true);
            yield return Toils_General.Do(() =>
            {
                StarPlatinumStand starPlatinumStand = pawn as StarPlatinumStand;
                starPlatinumStand.standAbilityTracker.GetAbilitie<TheWorldAbility>().Continue = true;
                starPlatinumStand.TheWorld = true;
                Find.TickManager.TogglePaused();
                Job job = JobMaker.MakeJob(StandJobDefOf.StandGhostFollow);
                starPlatinumStand.jobs.StartJob(job, JobCondition.Succeeded);
                Find.CameraDriver.StartCoroutine(TheWorldRun(starPlatinumStand));
            }
            );

        }

    }
}
